package com.phkj.qdh.utils.treela;


import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;


import com.phkj.qdh.R;
import com.phkj.qdh.utils.treela.Node;
import com.phkj.qdh.utils.treela.TreeListViewAdapter;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by keranbin on 2016/8/30.
 */
public class CheckBoxTreeListViewAdapter<T> extends TreeListViewAdapter {
    //判断checkbox是否是单选的标志
    private boolean isSingle = true;

    private static List<Node> nodeList;

    private OnTreeNodeChooseListener onTreeNodeChooseListener;

    /**
     * @param context           上下文对象
     * @param listView
     * @param datas
     * @param defaultExpandLevel  默认初始化时展开几层
     * @param isSingle              checkbox是不是单选的
     * @throws IllegalAccessException
     */
    public CheckBoxTreeListViewAdapter(Context context, ListView listView, List<T> datas, int defaultExpandLevel, boolean isSingle) throws IllegalAccessException {
        super(context, listView, datas, defaultExpandLevel);
        this.isSingle = isSingle;
    }


    @Override
    public View getConvertView(final Node node, int position, View view, ViewGroup viewGroup) {
        ViewHolder vh=null;
        if (view == null) {
            vh = new ViewHolder();
            view = inflater.inflate(R.layout.lv_tree_item, viewGroup, false);
            vh.ivIcon = (ImageView) view.findViewById(R.id.iv);
            vh.tvName = (TextView) view.findViewById(R.id.tv_name);
            vh.cbChoose = (CheckBox) view.findViewById(R.id.cb);
            view.setTag(vh);
        } else {
            vh = (ViewHolder) view.getTag();
        }


        //如果node的icon为-1,说明是叶子节点，隐藏图标，显示checkbox，否则显示相应的图标，隐藏checkbox
        if (node.getIcon() == -1) {
            vh.ivIcon.setVisibility(View.INVISIBLE);
            vh.cbChoose.setVisibility(View.VISIBLE);
            vh.cbChoose.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton compoundButton, boolean isCheck) {
                    if (isSingle) {  //如果checkbox是单选的
                        if (isCheck) { //如果checkbox的状态是选中的，那么除了被选中的那条数据，其他Node节点的checkbox状态都为false
                            for (int i = 0; i < mAllNodes.size(); i++) {
                                if (((Node) mAllNodes.get(i)).getId().equals(node.getId())) {
                                    ((Node) mAllNodes.get(i)).setChoose(isCheck);
                                } else {
                                    ((Node) mAllNodes.get(i)).setChoose(false);
                                }
                            }
                        } else {//如果checkbox的状态是选中的，所有Node节点checkbox状态都为false
                            for (int i = 0; i < mAllNodes.size(); i++) {
                                if (((Node) mAllNodes.get(i)).getId().equals(node.getId())) {
                                    ((Node) mAllNodes.get(i)).setChoose(isCheck);
                                }
                            }
                        }
                    } else {   ////如果checkbox是多选的，对应node节点的checkbox状态视用户的操作而定
                        for (int i = 0; i < mAllNodes.size(); i++) {
                            if (((Node) mAllNodes.get(i)).getId().equals(node.getId()))
                                ((Node) mAllNodes.get(i)).setChoose(isCheck);

                        }
                    }
                    onTreeNodeChooseListener.OnTreeNodeChoose(getSelectedNodes());//回调所选择的节点数据给用户
                    notifyDataSetChanged();
                }
            });
            vh.cbChoose.setChecked(node.isChoose());
        } else {
            vh.ivIcon.setVisibility(View.VISIBLE);
            vh.ivIcon.setImageResource(node.getIcon());
            vh.cbChoose.setVisibility(View.INVISIBLE);
        }
        vh.tvName.setText(node.getName());
        return view;
    }

    /**
     * 返回所选node集合
     * @return
     */
    public List<Node> getSelectedNodes(){
        nodeList=new ArrayList<>();
        for(int i=0;i<mAllNodes.size();i++){
            if(((Node)mAllNodes.get(i)).isChoose()){
                nodeList.add((Node) mAllNodes.get(i));
            }
        }
        return nodeList;
    }

    public void setOnTreedNodeChooseListener(OnTreeNodeChooseListener onTreeNodeChooseListener) {
        this.onTreeNodeChooseListener = onTreeNodeChooseListener;
    }

    public interface OnTreeNodeChooseListener {
        void OnTreeNodeChoose(List<Node> nodes);
    }

    class ViewHolder {
        private ImageView ivIcon;
        private TextView tvName;
        private CheckBox cbChoose;
    }
}